package com.zelin.interceptor;

import com.zelin.exception.MyException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * ------------------------------
 * 功能：登录拦截器
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/15-10:15
 * ------------------------------
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    //1. 登录前的处理
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.1 得到session
        HttpSession session = request.getSession();
        //1.2 得到session中的username
        Object username = session.getAttribute("username");
        if(username == null){       //代表未登录
            //session.setAttribute("message","你还未登录，请登录！");
            throw new MyException("你未登录，请登录！");
            //response.sendRedirect("/login.jsp");
            //return false;
        }else{                      //代表己登录就向下执行
            return true;
        }
    }
}
